/** @namespace */
var agr = agr || {};

/**
 *
 * functions are called before UI changes, if they return falsy the UI
 * is not changed.
 *
 * noedit disables the edit button
 *
 * omitting a back function binds it to window.history.back.
 *
 * @constructor
 * @param {jQuery} a_left top left button.
 * @param {jQuery} h1_title title.
 * @param {jQuery} a_right top right button.
 */
agr.Header = function(a_left, h1_title, a_right) {

    var EditShowing,
        disp_left = function(bool) {
            if (bool) {
                a_left.css('display','none');
            } else {
                a_left.css('display','inline-block');
            }
        },
        disp_right = function(bool) {
            if (bool) {
                a_right.css('display','none');
            } else {
                a_right.css('display','inline-block');
            }
        },
        right_fn,
        left_fn;

    this.hideLeft = function() {disp_left(true);}
    this.showLeft = function() {disp_left(false);}

    this.hideRight = function() {disp_right(true);}
    this.showRight = function() {disp_right(false);}

    this.showEdit = function() {
        a_right.find('.ui-btn-text').text('Edit');
        a_right.find('.ui-icon')
            .removeClass('ui-icon-check')
            .addClass('ui-icon-gear');

        a_left.find('.ui-btn-text').text('Back');

        EditShowing = true;
    }

    this.showDone = function() {
        a_right.find('.ui-btn-text').text('Done');
        a_right.find('.ui-icon')
            .removeClass('ui-icon-gear')
            .addClass('ui-icon-check');

        a_left.find('.ui-btn-text').text('Cancel');

        EditShowing = false;
    }

    this.setTitle = function(text) {
        h1_title.text(text);
    }

    a_left.click(
        function() {
            if (left_fn) left_fn();
        });

    a_right.click(
        function() {
            if (right_fn) right_fn();
        });

    this.clickLeft = function(f) {
        left_fn = f;
    }

    this.clickRight = function(f) {
        right_fn = f;
    }

    this.leftCancel = function() {
        return !EditShowing;
    }

    this.leftBack = function() {
        return EditShowing;
    }

    this.rightDone = function() {
        return !EditShowing;
    }

    this.rightEdit = function() {
        return EditShowing;
    }
};
